<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ZZN
 * Date: 13-7-3
 * Time: 下午2:22
 * To change this template use File | Settings | File Templates.
 */

include '../inc/common.simple.php';
$uid = 0;
if(isset($_GET['username'])){
    $query=mysql_fetch_array(mysql_query("select user_id from ".tname('user')." where urlname='".$_GET['username']."'"));
    $uid = $query['user_id'];
}
$do=isset($_GET['do']) && in_array($_GET['do'],array('follow','unfollow'))?$_GET['do']:'follow';
$followid=$session->get('uid');

if($followid>0 && $uid>0)
{
    $notice=mysql_fetch_array(mysql_query("select notice_id,user_id,follow_id,follow from ".tname("notice_follow")." where user_id=".$uid." and follow_id=".$followid));
    if(isset($notice['notice_id']) && $notice['notice_id']>0)
    {
        if($do=='follow')
        {
            if($notice['follow']==0)
            {
                mysql_query("update ".tname('notice_follow')." set follow=1 where notice_id=".$notice['notice_id']);
                mysql_query("update ".tname('user')." set follower_count=follower_count+1 where user_id=".$notice['user_id']);
                mysql_query("update ".tname('user')." set following_count=following_count+1 where user_id=".$notice['follow_id']);
                mysql_query("insert into ".tname('feeds')."(user_id,created_at,feedtype,feedpid) values('".$followid."','".time()."','follow_people','".$uid."')");
            }

        }
        elseif($do=='unfollow')
        {
            if($notice['follow']==1)
            {
                mysql_query("update ".tname('notice_follow')." set follow=0 where notice_id=".$notice['notice_id']);
                mysql_query("update ".tname('user')." set follower_count=follower_count-1 where user_id=".$notice['user_id']);
                mysql_query("update ".tname('user')." set following_count=following_count-1 where user_id=".$notice['follow_id']);
                mysql_query("delete from ".tname('feeds')." where feedpid='".$notice['user_id']."' and user_id=".$notice['follow_id']);
            }
        }
    }
    else
    {
        if($do=='follow')
        {
            mysql_query("insert into ".tname('notice_follow')."(follow_id,user_id,follow) values('".$followid."','".$uid."','1')");
            mysql_query("update ".tname('user')." set follower_count=follower_count+1 where user_id=".$uid);
            mysql_query("update ".tname('user')." set following_count=following_count+1 where user_id=".$followid);
            mysql_query("insert into ".tname('feeds')."(user_id,created_at,feedtype,feedpid) values('".$followid."','".time()."','follow_people','".$uid."')");
        }
    }
}
echo '1';
?>